<?php
#####################################
# * @Author: lehieu008              #
# * @Email: lehieu008@gmail.com     #
# * @Copyright: 2008 - 2010         #
#####################################
if(!defined('BASEPATH'))exit('No direct script access allowed');
class Product_module extends Controller
{
    function __construct()
    {
        parent::Controller();
    }
    
    function hot($home = true)
    {
        if($home == true)
        {
            $limit = 16;
        }
        else
        {
            $limit = 8;
        }
        $sql = "SELECT proID, proSection, proCategory, proName, proPrice, proCurrency, proImage, proDirImage FROM {PRE}product ";
        $sql .= "WHERE proImage != '' AND proStatus = 1 AND proHot >= ".time()." AND proBestShop >= ".time()." AND proExpireDate >= ".time()." ";
        $sql .= "ORDER BY RAND() LIMIT 0,$limit";
        $data['rows'] = $this->global_model->fetch($sql);
        $data['home'] = $home;
        #Load View
        $this->load->view('product/hot', $data);
    }
    
    function bestshop()
    {
        $sql = "SELECT proID, proSection, proCategory, proName, proPrice, proCurrency, proImage, proDirImage FROM {PRE}product ";
        $sql .= "WHERE proImage != '' AND proStatus = 1 AND proBestShop >= ".time()." AND proExpireDate >= ".time()." ";
        $sql .= "ORDER BY RAND() LIMIT 0,8";
        $data['rows'] = $this->global_model->fetch($sql);
        #Load View
        $this->load->view('product/bestshop', $data);
    }
    
    function saleoff()
    {
        $sql = "SELECT proID, proSection, proCategory, proName, proDetail, proPrice, proCurrency, proImage, proDirImage, proPostDate, shoLink, shoName, useFullname, useAddress, useEmail, usePhone FROM {PRE}product ";
        $sql .= "INNER JOIN {PRE}shop ON {PRE}product.proUser = {PRE}shop.shoUser ";
        $sql .= "INNER JOIN {PRE}user ON {PRE}product.proUser = {PRE}user.useID ";
        $sql .= "WHERE proImage != '' AND proSaleOff = 1 AND proStatus = 1 AND proExpireDate >= ".time()." ";
        $sql .= "ORDER BY RAND() LIMIT 0,".PRODUCT_MODULE_ROWS;
        $data['rows'] = $this->global_model->fetch($sql);
        #Load View
        $this->load->view('product/saleoff', $data);
    }
    
    function popular($home = true)
    {
        if($home == true)
        {
            $limit = 18;
        }
        else
        {
            $limit = PRODUCT_MODULE_ROWS;
        }
        $sql = "SELECT proID, proSection, proCategory, proName, proPrice, proCurrency, proImage, proDirImage, proVote, proCountVote, proPostDate FROM {PRE}product ";
        $sql .= "WHERE proImage != '' AND proStatus = 1 AND proExpireDate >= ".time()." ";
        $sql .= "ORDER BY proHot DESC, proBestShop DESC, proView DESC LIMIT 0,$limit";
        $data['rows'] = $this->global_model->fetch($sql);
        $data['home'] = $home;
        #Load View
        $this->load->view('product/popular', $data);
    }
    
    function user($user = 0, $current = 0)
    {
        $sql = "SELECT proID, proSection, proCategory, proName, proPrice, proCurrency, proImage, proDirImage, proVote, proCountVote, proPostDate FROM {PRE}product ";
        $sql .= "WHERE proID != $current AND proUser = $user AND proStatus = 1 AND proExpireDate >= ".time()." ";
        $sql .= "ORDER BY RAND() LIMIT 0,".PRODUCT_MODULE_ROWS;
        $data['rows'] = $this->global_model->fetch($sql);
        #Load View
        $this->load->view('product/user', $data);
    }
    
    function category($category = 0, $current = 0)
    {
        $sql = "SELECT proID, proSection, proCategory, proName, proDetail, proPrice, proCurrency, proImage, proDirImage, proPostDate, shoLink, shoName, useFullname, useAddress, useEmail, usePhone FROM {PRE}product ";
        $sql .= "INNER JOIN {PRE}shop ON {PRE}product.proUser = {PRE}shop.shoUser ";
        $sql .= "INNER JOIN {PRE}user ON {PRE}product.proUser = {PRE}user.useID ";
        $sql .= "WHERE proID != $current AND proCategory = $category AND proImage != '' AND proStatus = 1 AND proExpireDate >= ".time()." ";
        $sql .= "ORDER BY RAND() LIMIT 0,".PRODUCT_MODULE_ROWS;
        $data['rows'] = $this->global_model->fetch($sql);
        #Load View
        $this->load->view('product/category', $data);
    }
    
    function similar($categories = 0, $currents = 0)
    {
        $sql = "SELECT proID, proSection, proCategory, proName, proDetail, proPrice, proCurrency, proImage, proDirImage, proPostDate, shoLink, shoName, useFullname, useAddress, useEmail, usePhone FROM {PRE}product ";
        $sql .= "INNER JOIN {PRE}shop ON {PRE}product.proUser = {PRE}shop.shoUser ";
        $sql .= "INNER JOIN {PRE}user ON {PRE}product.proUser = {PRE}user.useID ";
        $sql .= "WHERE proID NOT IN($currents) AND proCategory IN($categories) AND proImage != '' AND proSaleOff = 1 AND proStatus = 1 AND proExpireDate >= ".time()." ";
        $sql .= "ORDER BY RAND() LIMIT 0,".PRODUCT_MODULE_ROWS;
        $data['rows'] = $this->global_model->fetch($sql);
        #Load View
        $this->load->view('product/similar', $data);
    }
}